package com.example.myapplication.adapter.recyclerViewAdapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.activity.BaseOnclickListener;
import com.example.myapplication.R;
import com.example.myapplication.entity.HotSongs;

import java.util.List;

public class HotSongAdapter extends RecyclerView.Adapter<HotSongAdapter.HotSongHolder>{
    private List<HotSongs>hotSongsList;
    private BaseOnclickListener baseOnclickListener;


    public void setData(List<HotSongs>hotSongsList){
        this.hotSongsList = hotSongsList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public HotSongHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_hotsong,parent,false);
        return new HotSongHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull HotSongHolder holder, int position) {
        HotSongs hotSongs = hotSongsList.get(position);
        if(hotSongs.getFee()!=0){
            holder.fee.setVisibility(View.GONE);
        }
        holder.singer.setText(hotSongs.getAr().get(0).getName()+"-"+hotSongs.getAl().getName());
        holder.name_hotsong.setText(hotSongs.getName());
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(baseOnclickListener !=null){
                    baseOnclickListener.onclick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return hotSongsList == null ? 0 :hotSongsList.size();
    }

    public void setBaseOnclickListener(BaseOnclickListener baseOnclickListener){
        this.baseOnclickListener = baseOnclickListener;
    }

    class HotSongHolder extends RecyclerView.ViewHolder {
        TextView fee;
        TextView desc;
        TextView name_hotsong;
        View view;
        TextView singer;
        public HotSongHolder(@NonNull View itemView) {
            super(itemView);
            name_hotsong = itemView.findViewById(R.id.name_hotsong);
            view = itemView.findViewById(R.id.view);
            fee = itemView.findViewById(R.id.fee);
            singer = itemView.findViewById(R.id.singer);
            desc = itemView.findViewById(R.id.desc);
        }
    }
}
